//
//  QQLoginTool.m
//  WSJBaseProject
//
//  Created by wsj2015 on 2016/12/2.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "QQLoginTool.h"
#import <TencentOpenAPI/TencentOAuth.h>

static QQLoginTool *qqloginTool = nil;

@interface QQLoginTool ()<TencentLoginDelegate>

@property (nonatomic,  strong) TencentOAuth *auth;

@property (nonatomic, copy) void (^successBlock)(NSDictionary *dict );

@property (nonatomic, copy) void (^failBlock)( );

@end

@implementation QQLoginTool




+ (instancetype)sharedTool{
    
    if (qqloginTool ==nil) {
        qqloginTool = [QQLoginTool new];
        NSString *appid = @"1105785009";
        qqloginTool.auth =  [[TencentOAuth alloc] initWithAppId:appid andDelegate:(id<TencentSessionDelegate>)qqloginTool];
    }
    return qqloginTool;
   

}


- (void)loginWithSucess:(void(^)(NSDictionary *))sucessBlock failBlock:(void(^)())failBlock{
    self.successBlock = sucessBlock;
    self.failBlock = failBlock;
    BOOL isHaveQq = [TencentOAuth iphoneQQInstalled];
    if (isHaveQq) {
        
        NSArray* permissions = [NSArray arrayWithObjects:
                                kOPEN_PERMISSION_GET_USER_INFO,
                                kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                                kOPEN_PERMISSION_ADD_ALBUM,
                                kOPEN_PERMISSION_ADD_ONE_BLOG,
                                kOPEN_PERMISSION_ADD_SHARE,
                                kOPEN_PERMISSION_ADD_TOPIC,
                                kOPEN_PERMISSION_CHECK_PAGE_FANS,
                                kOPEN_PERMISSION_GET_INFO,
                                kOPEN_PERMISSION_GET_OTHER_INFO,
                                kOPEN_PERMISSION_LIST_ALBUM,
                                kOPEN_PERMISSION_UPLOAD_PIC,
                                kOPEN_PERMISSION_GET_VIP_INFO,
                                kOPEN_PERMISSION_GET_VIP_RICH_INFO,
                                nil];
        [self.auth authorize:permissions];
    }else{
      
    }
   

}

- (void)tencentDidLogin{
    [self.auth getUserInfo];
    
}
/**
 * 登录失败后的回调
 * \param cancelled 代表用户是否主动退出登录
 */
- (void)tencentDidNotLogin:(BOOL)cancelled{
    self.failBlock();
}

/**
 * 登录时网络有问题的回调
 */
- (void)tencentDidNotNetWork{
    
    self.failBlock();
    
}
- (void)getUserInfoResponse:(APIResponse*) response{
    
    NSDictionary *dic =response.jsonResponse;
    NSLog(@"%@",dic);
    self.successBlock(dic);
}



@end
